// 导入链接数据库的模板
const connection = require('./model/connection_model')
connection.connection(success)

async function success() {
// 导入express
    const express = require('express');
// 创建路由对象
    const app = express();

//导入集合
    const books = require('./model/集合')
    const booksModel = books.bookModel()

// 导入获取 请求体 模块
    const request = require('./model/获取请求体')

//添加CORS中间件 允许跨域
    app.use(function (req, res, next) {
        res.header('Access-Control-Allow-Origin', 'http://localhost:63342'); // 将请求源设置为允许的来源
        res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); // 允许的HTTP方法
        res.header('Access-Control-Allow-Headers', 'Content-Type'); // 允许的请求头
        next();
    });

// 创建 查找 路由
    app.get('/found', async (req, res) => {
        //获取请求查询字符串
        let str = req.query
        //批量获取
        try {
            let data = await booksModel.find(str).select({__v: 0, _id: 0}).exec()
            setTimeout(() => {
                console.log('读取成功~\n' + data)
                res.send(data);
            }, 2000);
        } catch (err) {
            console.log(err)
        }
    });

// 创建 新增 路由
    app.post('/create', async (req, res, next) => {
        //获取请求体
        request(req, res, next, async fields => {
            // // 新增
            setTimeout(async () => {
                try {
                    let data = await booksModel.create({
                        name: fields.name[0],
                        author: fields.author[0],
                        author_sex: fields.author_sex[0],//这里的值必须是枚举数组中的值
                        price: fields.price[0],
                        is_hot: fields.is_hot[0],//是否是热销书籍
                        tags: fields['tags[]'].join(','),//书籍标签
                        save_time: fields.save_time[0],//时间
                        test: fields.test[0]
                    })
                    console.log('添加成功')
                    res.send(data)

                } catch (err) {
                    console.log(err)
                }
            }, 2000)

        })
    });


// 创建 更新 路由
    app.put('/change', async (req, res, next) => {
        //获取请求体
        request(req, res, next, async fields => {
            let resStr = fields.test[0]
            try {
                // 更新test字段
                await booksModel.updateMany({test: resStr})
                //批量获取
                let data = await booksModel.find({test: resStr}).select({__v: 0, _id: 0}).exec()
                setTimeout(() => {
                    res.send(data);
                }, 1000);
            } catch (err) {
                console.log(err)
            }
        })
    });

// 创建 删除 路由
    app.delete('/delete', async (req, res, next) => {
        request(req, res, next, async fields => {
            //获取请求体
            let resStr = fields.name[0]
            //批量获取
            try {
                let data = await booksModel.deleteMany({name: resStr}).exec()
                res.send(data);
            } catch (err) {
                console.log(err)
            }
        })
    });


// 启动路由
    app.listen(9000, () => {
        console.log('启动成功~监听9000端口！');
    });
}
